//=============================================================================
//
//   File : qtg_xml_reader.cpp
//   Creation date : Sab 19 mar 2011 09:31:42 CET by Antonino G.Imbesi
//
//   This file is part of the QtGLauncher distribution
//   Copyright (C) 2011 Antonino G. Imbesi <grifisx at barmes dot org>
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include <QIcon>
#include <QXmlStreamReader>
#include <QTreeWidget>
#include <QMap>
#include <QDebug>
#include "qtg_listviewitem.h"
#include "qtg_xml_reader.h"

qtg_xml_reader::qtg_xml_reader(QTreeWidget *pTreeWidget)
    : m_pTreeWidget(pTreeWidget)
{
    m_pXmlStream = new QXmlStreamReader;
}

bool qtg_xml_reader::read(QIODevice *pDevice)
{
    m_pXmlStream->setDevice(pDevice);

    if (xmlreadNextStartElement()) {
        if (m_pXmlStream->name() == "QtGLauncher" && m_pXmlStream->attributes().value("Version") == "1.5.2")
            readXBEL();
        else
            m_pXmlStream->raiseError(QObject::tr("The file is not an QtGLauncher version 1.5.2 file."));
    }

    return !m_pXmlStream->error();
}

QString qtg_xml_reader::errorString() const
{
    return QObject::tr("%1\nLine %2, column %3")
            .arg(m_pXmlStream->errorString())
            .arg(m_pXmlStream->lineNumber())
            .arg(m_pXmlStream->columnNumber());
}

void qtg_xml_reader::readXBEL()
{

    qDebug ()<< "readXBEL" << m_pXmlStream->name();
    while (xmlreadNextStartElement())
    {
        if (m_pXmlStream->name() == "Category")
            readFolder(0);
        else if (m_pXmlStream->name() == "Software")
            readBookmark(0);
        else if (m_pXmlStream->name() == "Separator")
            readSeparator(0);
        else
            xmlskipCurrentElement();
    }
}


void qtg_xml_reader::readSeparator(qtg_QTreeWidgetItem *pItem)
{
    qtg_QTreeWidgetItem *pSeparator = createChildItem(pItem);
    Qt::ItemFlags flags=pSeparator->flags();
    pSeparator->setFlags(flags & ~Qt::ItemIsSelectable);
    pSeparator->setText(0, QString(30, 0xB7));
    xmlskipCurrentElement();
}


void qtg_xml_reader::readFolder(qtg_QTreeWidgetItem *pItem)
{
    if(m_pXmlStream->attributes().value("name")=="" )
    {
        if (xmlreadNextStartElement()) readFolder(0);
        return;
    }

    qtg_QTreeWidgetItem *pFolder = createChildItem(pItem);
    pFolder->setText(0, m_pXmlStream->attributes().value("name").toString());
    m_szCategory=m_pXmlStream->attributes().value("name").toString();
    QString szIconPath = m_pXmlStream->attributes().value("pathicon").toString();
    pFolder->setIcon(0, QIcon(szIconPath));
    while (xmlreadNextStartElement()) {
        if (m_pXmlStream->name() == "Category")
            readFolder(pFolder);
        else if (m_pXmlStream->name() == "Software")
            readBookmark(pFolder);
        else if (m_pXmlStream->name() == "Separator")
            readSeparator(pFolder);
        else
            xmlskipCurrentElement();
    }
}

void qtg_xml_reader::readBookmark(qtg_QTreeWidgetItem *pItem)
{

    qtg_QTreeWidgetItem *pBookmark = createChildItem(pItem);

    pBookmark->setText(0, m_pXmlStream->attributes().value("name").toString());
    m_szSoftware=m_pXmlStream->attributes().value("name").toString();
    m_xmlMap.insert(m_szSoftware,m_szCategory);

    QString szIconPath = m_pXmlStream->attributes().value("pathicon").toString();
    pBookmark->setIcon(0, QIcon(szIconPath));
    pBookmark->m_szIconPath=szIconPath;

    QString szEexePath = m_pXmlStream->attributes().value("pathexe").toString();
    pBookmark->setExecInfo(szEexePath);

    QString szExeParams = m_pXmlStream->attributes().value("exeparams").toString() ;
    pBookmark->setExecParameters(szExeParams);

    xmlskipCurrentElement();
}

qtg_QTreeWidgetItem *qtg_xml_reader::createChildItem(qtg_QTreeWidgetItem *pItem)
{
    qtg_QTreeWidgetItem *pChildItem;

    if (pItem)
    {
        pChildItem = new qtg_QTreeWidgetItem(pItem);
    } else
    {
        pChildItem = new qtg_QTreeWidgetItem(m_pTreeWidget);
    }
    return pChildItem;
}

bool qtg_xml_reader::xmlreadNextStartElement()
{
    // This functions are to emulate new xml functions in old QT
    if(QT_VERSION > 4.7){
        //    qDebug () << "Qt > 4.7";
        return m_pXmlStream->readNextStartElement();}
    else{
        //   qDebug () << "Qt < 4.7";
        while (m_pXmlStream->readNext() != QXmlStreamReader::Invalid) {
            if (m_pXmlStream->isEndElement())
                return false;
            else if (m_pXmlStream->isStartElement())
                return true;
        }
        return false;
    }

}

void qtg_xml_reader::xmlskipCurrentElement()
{
    // This functions are to emulate new xml functions in old QT
    if(QT_VERSION > 4.7){
        m_pXmlStream->skipCurrentElement();
    }
    else
    {
        while (xmlreadNextStartElement())
            xmlskipCurrentElement();
    }

}
QStringList qtg_xml_reader::getAllCategoryNames()
{
    QStringList list = m_xmlMap.values();
    list.removeDuplicates();
    return list;
}

QStringList qtg_xml_reader::getSoftwareNodesByCategoryName(QString szCategory)
{
    return m_xmlMap.keys(szCategory);
}
